<?php

class View {

    public $template_dir = '';
    public $compile_dir = '';
    public $cache_dir = '';
    public $caching = 0;
    public $cache_lifetime = 0;
    public $left_delimiter = '<{';
    public $right_delimiter = '}>';
    public $template_ext = '.html';
    public $compilefile = '';
    public $templatefile = '';
    public $cachefile = '';
    public $includeNum = 0;
    public $tplVarsArr = array();

    //初始化缓存信息
    public function __construct($caching = false, $cache_lifetime = 0) {
	$this->template_dir = APP_PATH . '/views/' . $_GET['module'] . '/';
	$this->compile_dir = APP_PATH . '/views_c/' . $_GET['module'] . '/';
	$this->cache_dir = APP_PATH . '/cache/' . $_GET['module'] . '/';
	$this->caching = $caching;
	$this->cache_lifetime = $cache_lifetime;
    }

    //设置属性值
    public function __set($name, $value) {
	$this->tplVarsArr[$name] = $value;
    }

    //获取属性值
    public function __get($name) {
	if (isset($this->tplVarsArr[$name])) {
	    return($this->tplVarsArr[$name]);
	} else {
	    return NULL;
	}
    }

    public function includeCompileFile($templateName) {
	$this->templatefile = $this->template_dir . $templateName . $this->template_ext;
	$this->compilefile = $this->compile_dir . $templateName . '.php';
	if (!@file_exists($this->compilefile) || (@filemtime($this->compilefile) < @filemtime($this->templatefile))) {
	    self::compile($this->templatefile, $this->compilefile);
	}
	if ($this->includeNum == 0) {
	    @extract($this->tplVarsArr);
	    $this->includeNum++;
	}
	include($this->compilefile);
    }

    public static function compile($templatefile, $compilefile) {
	$templateContent = @file_get_contents($templatefile);
	$template = array(
	    '/<\{\s*include=\s*(.*)\.html\s*\}>/isU',
	    '/<\{\s*if(.*)\s*\}>/isU',
	    '/<\{\s*elseif(.*)\s*\}>/isU',
	    '/<\{\s*else(.*)\s*\}>/isU',
	    '/<\{\s*\/if\s*\}>/isU',
	    '/<\{\s*foreach\s*key=(.*)\s*item=(.*)\s*from=(.*)\s*\s*\}>/isU',
	    '/<\{\s*\/foreach\s*\}>/isU',
	    '/<\{\s*(.*)\s*\}>/isU',
	);

	$compile = array(
	    '<?php $this->includeCompileFile("\\1"); ?>',
	    '<?php if (\\1) {?>',
	    '<?php }elseif (\\1){ ?>',
	    '<?php }else {?>',
	    '<?php }?>',
	    '<?php foreach(\\3 as \\1 => \\2) {?>',
	    '<?php }?>',
	    '<?php echo \\1; ?>',
	);
	//多层嵌套怎么处理 ,嵌套模板修改重新编译判断？？
	$compileContent = preg_replace($template, $compile, $templateContent);
	@file_put_contents($compilefile, $compileContent);
	//echo $compilefile.'编译文件生成</BR>';
	return $compileContent;
    }

    public function is_cached($templateName, $cacheid = '') {
	//echo $cacheid;
	$this->cachefile = $this->cache_dir . $cacheid . $templateName . $this->template_ext;
	//echo $this->cachefile;
	if ($this->caching) {
	    if (@file_exists($this->cachefile) && (time() - @filemtime($this->cachefile)) < $this->cache_lifetime) {
		return true;
	    }
	}
	return false;
    }

    public function assign($name, $value = '') {
	if (is_array($name)) {
	    $this->tplVarsArr = array_merge($this->tplVarsArr, $name);
	} elseif (is_object($name)) {
	    foreach ($name as $key => $val)
		$this->tplVarsArr[$key] = $val;
	} else {
	    $this->tplVarsArr[$name] = $value;
	}
	//$this->tplVarsArr[$name] = $value; 
	return $this;
    }

    public function filter($pattern, $replacement, $filterssContent) {
	
    }

    public function createCacheContent($compilefile) {
	ob_start();
	ob_implicit_flush(false);
	$this->includeCompileFile($compilefile);
	$cacheContent = ob_get_contents();
	ob_end_clean();
	return $cacheContent;
    }

    public function display($templateName, $cacheid = '') {
	echo $this->feach($templateName, $cacheid);
    }

    public function feach($templateName, $cacheid = '') {
	$this->cachefile = $this->cache_dir . $cacheid . $templateName . $this->template_ext;
	$this->compilefile = $this->compile_dir . $templateName . '.php';
	if ($this->caching) {
	    if (!$this->is_cached($templateName, $cacheid)) {
		$cacheContent = $this->createCacheContent($templateName);
		@file_put_contents($this->cachefile, $cacheContent);
		//echo $this->cachefile.'缓存文件生成</BR>';   
	    } else {
		$cacheContent = @file_get_contents($this->cachefile);
	    }
	} else {
	    $cacheContent = $this->createCacheContent($templateName);
	}
	return $cacheContent;
    }

    public static function initSmarty() {
	include(SYS_PATH . '/lib/smarty/Smarty.class.php');
	$smarty = new Smarty();
	$smarty->left_delimiter = '<{';
	$smarty->right_delimiter = '}>';
	$smarty->template_dir = APP_PATH . '/views/';
	$smarty->compile_dir = APP_PATH . '/views_c/';
	$smarty->cache_dir = APP_PATH . '/cache/';
	$smarty->caching = 0;
	$smarty->cache_lifetime = 0;
	return $smarty;
    }

}